package webserviceServer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Vector;

import Tracker.Data.DataBase.ConnectionToDataBase;
import Tracker.Data.DataBaseManagement.DataManagement;

import common.User;


import webserviceP2PClient.WebServiceClientMethodsProxy;






public class WebServiceServerImpl {
	

	private DataManagement database;
	
	
	
	public WebServiceServerImpl(DataManagement d){
		database = d ;
	}
	
	public boolean register(byte[] bytes){
		
		
		try{
			
			User user = (User)arrayBytesToObject(bytes);
			
			database.AddUser(user);
			
			return true;
		}
		catch(Exception e){
			
			e.printStackTrace();
			
			return false;
		}
		
		
		
		
		
	}
	
	public byte[] login(String nickname, String pass,String ipPort){
		
		
			try {
				
				System.out.println("entrou no servidor"+nickname);
				
				database = new DataManagement();
				
				User user = ConnectionToDataBase.GetUser(nickname);
				
				Vector<String> ipPorts;
				
				if(user.getPassword().equals(pass)){
					
					//user.setOnline(true);
					
					//User userOnline=database.editUser(nickname,user);
					
					ipPorts = database.GetAllDHTIp();
					
					if(ipPorts.capacity()==0){//nao ha nenhum user na rede DHT
						
						database.addDHTUser(nickname, ipPort);
						
						ipPorts.add(ipPort);
						
						return createArrayBytes(ipPorts);
					}
					
					return createArrayBytes(ipPorts);
				}
					
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
			
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
			
				e.printStackTrace();
			
			}catch (NullPointerException e){
				
				
				
				e.printStackTrace();
				
				return null;
			}
			
		
		
		return null;
	
		
		
	}
	
//	private boolean emptyDHT(){
//		
//		try {
//			
//			Vector<String> users = database.GetAllDHTIp();
//			
//			boolean isEmpty=true;
//			
//			for(int i=0;i<users.size();++i){
//				
//				User user = users.get(i);
//				
//				if(wait(timeout, nanos))
//				
//			}
//		
//		} catch (SQLException e) {
//		
//			e.printStackTrace();
//		}
//		
//	}
	
	
	public Vector<String>sayHello(byte[] bytes){
		
//		WebServiceClientMethodsProxy proxyClient =  new WebServiceClientMethodsProxy();
//		
//		try {
//		ByteArrayInputStream baip = new ByteArrayInputStream(bytes);	
//			
//		ObjectInputStream ois = new ObjectInputStream(baip);
//		
//		User user =(User)ois.readObject();
//		
//		System.out.println("recebido pedido no servidor:"+user.getUserName());
//		
//			proxyClient.publicInCliente("envio do string do servidor");
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
		System.out.println("pedido do cliente");
		
		Vector<String> vector = new Vector<String>();
		vector.add("gon");
		vector.add("cat");
		
		return vector;
	}
	private byte[] createArrayBytes(Object o){
		
		try {
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			
			oos.writeObject(o);
		
			byte[] workAsBytes = baos.toByteArray();
			
			return workAsBytes;
			
		
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
		
		
	}
	private Object arrayBytesToObject(byte[] bytes){
		
		ByteArrayInputStream baip = new ByteArrayInputStream(bytes);	

		try {
			
			ObjectInputStream ois = new ObjectInputStream(baip);
		
			Object object = ois.readObject();
			
			return object;
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
}
